/*
 * ConsultarVacas.java
 *
 * Created on 09-may-2012, 15:56:03
 */

package interfazgrafica.consultar.vacas;

import interfazgrafica.ayuda.VentanaAyuda;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Lauaa
 */
public class ConsultarVacas extends javax.swing.JFrame {
    private final Connection conn;
    private final Statement stmt;

    /** Creates new form ConsultarVacas */
    public ConsultarVacas() throws ClassNotFoundException, SQLException {
        initComponents();
        //Accedo a la base de datos.
            Class.forName("oracle.jdbc.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "system", "terrano89");
            stmt = conn.createStatement();

        //Hago una consulta y la guardo en rs.
	ResultSet rs = null;
            rs = stmt.executeQuery("SELECT numeroid, fechanacimiento, numeroenexplotacion FROM Vacas");

                DefaultTableModel modelo = new DefaultTableModel();
                jTable1.setModel(modelo);

                // Creamos las columnas.
                modelo.addColumn("id");
                modelo.addColumn("Fecha nacimiento");
                modelo.addColumn("Numero en explotacion");

                // Bucle para cada resultado en la consulta
                while (rs.next())
                {
                   // Se crea un array que será una de las filas de la tabla.
                   Object [] fila = new Object[3]; // Hay tres columnas en la tabla

                   // Se rellena cada posición del array con una de las columnas de la tabla en base de datos.
                   for (int i=0;i<3;i++)
                      fila[i] = rs.getObject(i+1); // El primer indice en rs es el 1, no el cero, por eso se suma 1.

                   // Se añade al modelo la fila completa.
                   modelo.addRow(fila);
                }
                jTable1.setModel(modelo);

            rs.close();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabelPPadre = new javax.swing.JLabel();
        jLabelNumero = new javax.swing.JLabel();
        jTextFieldMadre = new javax.swing.JTextField();
        jLabelMadre = new javax.swing.JLabel();
        jTextFieldPadre = new javax.swing.JTextField();
        jTextFieldFechaNacimiento = new javax.swing.JTextField();
        jTextFieldId = new javax.swing.JTextField();
        jLabelFormato = new javax.swing.JLabel();
        jLabelRecalificacion = new javax.swing.JLabel();
        jLabelPadre = new javax.swing.JLabel();
        jLabelCalificacion = new javax.swing.JLabel();
        jLabelId = new javax.swing.JLabel();
        jTextFieldMMadre = new javax.swing.JTextField();
        jLabelMPadre = new javax.swing.JLabel();
        jTextFieldMPadre = new javax.swing.JTextField();
        jLabelPMadre = new javax.swing.JLabel();
        jTextFieldCalificacion = new javax.swing.JTextField();
        jTextFieldNumero = new javax.swing.JTextField();
        jTextFieldRecalificacion = new javax.swing.JTextField();
        jTextFieldPMadre = new javax.swing.JTextField();
        jTextFieldPPadre = new javax.swing.JTextField();
        jLabelFechaNacimiento = new javax.swing.JLabel();
        jLabelMMadre = new javax.swing.JLabel();
        jButtonAyuda = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cowmanager.CowManagerApp.class).getContext().getResourceMap(ConsultarVacas.class);
        jLabelPPadre.setText(resourceMap.getString("jLabelPPadre.text")); // NOI18N
        jLabelPPadre.setName("jLabelPPadre"); // NOI18N

        jLabelNumero.setText(resourceMap.getString("jLabelNumero.text")); // NOI18N
        jLabelNumero.setName("jLabelNumero"); // NOI18N

        jTextFieldMadre.setName("jTextFieldMadre"); // NOI18N

        jLabelMadre.setText(resourceMap.getString("jLabelMadre.text")); // NOI18N
        jLabelMadre.setName("jLabelMadre"); // NOI18N

        jTextFieldPadre.setName("jTextFieldPadre"); // NOI18N

        jTextFieldFechaNacimiento.setName("jTextFieldFechaNacimiento"); // NOI18N

        jTextFieldId.setName("jTextFieldId"); // NOI18N

        jLabelFormato.setText(resourceMap.getString("jLabelFormato.text")); // NOI18N
        jLabelFormato.setName("jLabelFormato"); // NOI18N

        jLabelRecalificacion.setText(resourceMap.getString("jLabelRecalificacion.text")); // NOI18N
        jLabelRecalificacion.setName("jLabelRecalificacion"); // NOI18N

        jLabelPadre.setText(resourceMap.getString("jLabelPadre.text")); // NOI18N
        jLabelPadre.setName("jLabelPadre"); // NOI18N

        jLabelCalificacion.setText(resourceMap.getString("jLabelCalificacion.text")); // NOI18N
        jLabelCalificacion.setName("jLabelCalificacion"); // NOI18N

        jLabelId.setText(resourceMap.getString("jLabelId.text")); // NOI18N
        jLabelId.setName("jLabelId"); // NOI18N

        jTextFieldMMadre.setName("jTextFieldMMadre"); // NOI18N

        jLabelMPadre.setText(resourceMap.getString("jLabelMPadre.text")); // NOI18N
        jLabelMPadre.setName("jLabelMPadre"); // NOI18N

        jTextFieldMPadre.setName("jTextFieldMPadre"); // NOI18N

        jLabelPMadre.setText(resourceMap.getString("jLabelPMadre.text")); // NOI18N
        jLabelPMadre.setName("jLabelPMadre"); // NOI18N

        jTextFieldCalificacion.setName("jTextFieldCalificacion"); // NOI18N

        jTextFieldNumero.setName("jTextFieldNumero"); // NOI18N

        jTextFieldRecalificacion.setName("jTextFieldRecalificacion"); // NOI18N

        jTextFieldPMadre.setName("jTextFieldPMadre"); // NOI18N

        jTextFieldPPadre.setName("jTextFieldPPadre"); // NOI18N

        jLabelFechaNacimiento.setText(resourceMap.getString("jLabelFechaNacimiento.text")); // NOI18N
        jLabelFechaNacimiento.setName("jLabelFechaNacimiento"); // NOI18N

        jLabelMMadre.setText(resourceMap.getString("jLabelMMadre.text")); // NOI18N
        jLabelMMadre.setName("jLabelMMadre"); // NOI18N

        jButtonAyuda.setText(resourceMap.getString("jButtonAyuda.text")); // NOI18N
        jButtonAyuda.setName("jButtonAyuda"); // NOI18N
        jButtonAyuda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAyudaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonAyuda, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabelRecalificacion)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldRecalificacion, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(255, 255, 255))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabelCalificacion)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldCalificacion, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                        .addGap(255, 255, 255))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelNumero)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldNumero, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelMadre)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldMadre, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelPadre)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldPadre, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelId)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelFechaNacimiento)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldFechaNacimiento, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelPPadre)
                                    .addComponent(jLabelPMadre)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(2, 2, 2)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabelMMadre)
                                            .addComponent(jLabelMPadre))))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextFieldMMadre, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                                    .addComponent(jTextFieldPPadre, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldPMadre, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                                    .addComponent(jTextFieldMPadre, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelFormato)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 536, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(314, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelId)
                    .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelPadre)
                    .addComponent(jTextFieldPadre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelPPadre)
                    .addComponent(jTextFieldPPadre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelPMadre)
                    .addComponent(jTextFieldPMadre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(98, 98, 98)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelNumero)
                            .addComponent(jTextFieldNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelFechaNacimiento)
                            .addComponent(jTextFieldFechaNacimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelFormato))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelCalificacion)
                            .addComponent(jTextFieldCalificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelRecalificacion)
                            .addComponent(jTextFieldRecalificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelMadre)
                            .addComponent(jTextFieldMadre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelMPadre)
                            .addComponent(jTextFieldMPadre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelMMadre)
                            .addComponent(jTextFieldMMadre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(21, 21, 21)
                .addComponent(jButtonAyuda)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(442, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        try {
            //Hago una consulta y la guardo en rs2.
            ResultSet rs2 = null;
            String consulta = "SELECT * FROM Vacas WHERE numeroid = '";
            consulta = consulta + jTable1.getValueAt(jTable1.getSelectedRow(), 0) + "'";
            rs2 = stmt.executeQuery(consulta);
            while (rs2.next()) {
                jTextFieldId.setText(rs2.getString("numeroid"));
                jTextFieldCalificacion.setText(rs2.getString("calificacion"));
                jTextFieldRecalificacion.setText(rs2.getString("recalificacion"));
                jTextFieldFechaNacimiento.setText(rs2.getString("fechanacimiento"));
                jTextFieldMMadre.setText(rs2.getString("mmadre"));
                jTextFieldMPadre.setText(rs2.getString("mpadre"));
                jTextFieldMadre.setText(rs2.getString("madre"));
                jTextFieldNumero.setText(rs2.getString("numeroenexplotacion"));
                jTextFieldPMadre.setText(rs2.getString("pmadre"));
                jTextFieldPPadre.setText(rs2.getString("ppadre"));
                jTextFieldPadre.setText(rs2.getString("padre"));
            }
            rs2.close();
        } catch (SQLException ex) {
            Logger.getLogger(ConsultarVacas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jTable1MouseClicked

    private void jButtonAyudaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAyudaActionPerformed
        VentanaAyuda ayuda = new VentanaAyuda("Consultar vacas");
        ayuda.setVisible(true);
    }//GEN-LAST:event_jButtonAyudaActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new ConsultarVacas().setVisible(true);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ConsultarVacas.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(ConsultarVacas.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAyuda;
    private javax.swing.JLabel jLabelCalificacion;
    private javax.swing.JLabel jLabelFechaNacimiento;
    private javax.swing.JLabel jLabelFormato;
    private javax.swing.JLabel jLabelId;
    private javax.swing.JLabel jLabelMMadre;
    private javax.swing.JLabel jLabelMPadre;
    private javax.swing.JLabel jLabelMadre;
    private javax.swing.JLabel jLabelNumero;
    private javax.swing.JLabel jLabelPMadre;
    private javax.swing.JLabel jLabelPPadre;
    private javax.swing.JLabel jLabelPadre;
    private javax.swing.JLabel jLabelRecalificacion;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldCalificacion;
    private javax.swing.JTextField jTextFieldFechaNacimiento;
    private javax.swing.JTextField jTextFieldId;
    private javax.swing.JTextField jTextFieldMMadre;
    private javax.swing.JTextField jTextFieldMPadre;
    private javax.swing.JTextField jTextFieldMadre;
    private javax.swing.JTextField jTextFieldNumero;
    private javax.swing.JTextField jTextFieldPMadre;
    private javax.swing.JTextField jTextFieldPPadre;
    private javax.swing.JTextField jTextFieldPadre;
    private javax.swing.JTextField jTextFieldRecalificacion;
    // End of variables declaration//GEN-END:variables

}
